package com.zhongxi.guoyuan.OffLineShop.Adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.comment.CommentData;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.seller.offline.SellerOffLineAdapter;
import com.zhongxi.guoyuan.widget.screenadaptaion.CustomLinearlayout;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/13.
 */

public class EvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_ITEM = 2;
    public static final int LOADING = 3;
    public static final int LOADCOMPLENT = 4;
    public static final int LOADEND = 5;
    private List<CommentData> commentDataList;
    private JSONObject user;
    private int loadingState;

    public EvaluateAdapter(List<CommentData> commentData) {
        this.commentDataList = commentData;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == commentDataList.size()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loadmore_footer, parent, false));
        } else {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_evaluate, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof MyViewHolder) {
            MyViewHolder holder = (MyViewHolder) viewHolder;
            CommentData mCommentData = commentDataList.get(position);
            holder.tvComment.setText(mCommentData.getContent());
            if (!TextUtils.isEmpty(String.valueOf(mCommentData.getStarlevel()))) {
                holder.rb.setRating(Float.parseFloat(mCommentData.getStarlevel()));
            }
            holder.tvTime.setText(mCommentData.getCtime());
            String name = user.optString(mCommentData.getUserid());
            if (!TextUtils.isEmpty(name)) {
                holder.tvPublish.setText(name);
            }
        } else if (viewHolder instanceof FooterViewHolder) {
            FooterViewHolder mHolder = (FooterViewHolder) viewHolder;
            switch (loadingState) {
                case LOADING:  // 正在加载
                    mHolder.mLoading.setVisibility(View.VISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADCOMPLENT:  // 加载完成
                    mHolder.mLoading.setVisibility(View.INVISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADEND:  //  记载到底
                    mHolder.mLoading.setVisibility(View.GONE);
                    mHolder.mLoadComplent.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (commentDataList != null && commentDataList.size() > 0) {
            return commentDataList.size() + 1;
        }
        return 0;
    }

    public void setUserInfo(JSONObject user) {
        this.user = user;
    }

    /**
     * 加载的状态
     *
     * @param loadingState
     */
    public void setLoadingState(int loadingState) {
        this.loadingState = loadingState;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tvComment)
        TextView tvComment;
        @Bind(R.id.rb)
        RatingBar rb;
        @Bind(R.id.tvTime)
        TextView tvTime;
        @Bind(R.id.tvPublish)
        TextView tvPublish;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ll_loading)
        CustomLinearlayout mLoading;
        @Bind(R.id.tv_loadcomplent)
        TextView mLoadComplent;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
